<?php

/**
 * Abstract set of Admin_Values
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Set.php 38 2009-02-18 17:12:39Z monzee $
 */
abstract class Admin_Model_Set implements Countable, IteratorAggregate, ArrayAccess
{
    protected $_set = array();

    public function __construct($data = null)
    {
        if (isset($data['data'])) {
            foreach ($data['data'] as $value) {
                $this->addValue($value);
            }
        }
    }

    public function addValue(array $data)
    {
        $row = new $this->_valueClass;
        $row->setData($data);

        $this->_set[] = $row;
    }

    public function toArray()
    {
        $ret = array();
        foreach ($this->_set as $value) {
            $ret[] = $value->toArray();
        }
        return $ret;
    }

    /**
     * Iterator aggregate implementation
     *
     * @return ArrayIterator
     */
    public function getIterator()
    {
        return new ArrayIterator($this->_set);
    }

    /**
     * Countable implementation
     *
     * @return int
     */
    public function count()
    {
        return count($this->_set);
    }

    /**
     * Array access implementation
     *
     * @param mixed $offset
     */
    public function offsetExists($offset)
    {
        return isset($this->_set[$offset]);
    }

    /**
     * Array access implementation
     *
     * @param mixed $offset
     */
    public function offsetGet($offset)
    {
        return $this->_set[$offset];
    }

    /**
     * Array access implementation
     *
     * @param mixed $offset
     * @param mixed $value
     */
    public function offsetSet($offset, $value)
    {
        $this->addValue($value);
    }

    /**
     * Array access implementation
     *
     * @param mixed $offset
     */
    public function offsetUnset($offset)
    {
        unset($this->_set[$offset]);
    }
}
